Page({
    data: {
        showLoadingState:false,
        //图片
        p11:"https://pic.imgdb.cn/item/62e2193ef54cd3f937581332.png",
        sportsPicUrlArray:["https://pic.imgdb.cn/item/632e769f16f2c2beb18f1bba.png",
        "https://pic.imgdb.cn/item/632e769f16f2c2beb18f1be7.png",
        "https://pic.imgdb.cn/item/632e769f16f2c2beb18f1bf0.png",
        "https://pic.imgdb.cn/item/632e769f16f2c2beb18f1bf8.png",
        "https://pic.imgdb.cn/item/632e769f16f2c2beb18f1bac.png",
        "https://pic.imgdb.cn/item/632e76cb16f2c2beb18f486c.png",
        "https://pic.imgdb.cn/item/632e76cb16f2c2beb18f48b2.png",
        "https://pic.imgdb.cn/item/632e76cb16f2c2beb18f4817.png",],
        //房间展示及数据转换
        room_state_all:['组队中','未评价','已评价'],
        rooms:[{
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:0,
            room_sports:2,
        },
        {
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:1,
            room_sports:2,
        },
        {
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:2,
            room_sports:2,
        },
        {
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:1,
            room_sports:2,
        },
        {
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:1,
            room_sports:2,
        },
        {
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:1,
            room_sports:2,
        },
        {
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:1,
            room_sports:2,
        },
        {
            room_id:123,
            room_location:'大学生活动中心篮球场',
            room_playground:'一号篮球场',
            room_date:{
                year:2022,
                month:7,
                day:10,
                starttime:16,
                endtime:20,
            },
            lack_person:5,
            room_state:0,
            room_sports:2,
        }
        ],
    },
    onLoad:function(options) {
        //获取所有房间信息
        var that = this;
        that.onReloadRoom();

    },
    onChangeResult:function(e){
        var that = this;
        var index = e.currentTarget.dataset.index;
        //如果是已加入的房间，则加入
        if(e.currentTarget.dataset.state == 1){
            wx.showModal({
              title:"这场您输了还是赢了？",
              confirmText:'赢了',
              cancelText:"输了",
              success(res){
                //将赢的结果输入到服务器
                that.onShowLoading();
                wx.request({
                  url: getApp().globalData.serverUrl,
                  method:"GET",
                  timeout:10000,
                  data:{
                    //各种数据的传送
                    operation:"WIN_LOSE",
                    win:res.confirm,
                    studentId:getApp().globalData.userInfo.studentId,
                    roomId:that.data.rooms[index].room_id,
                  },
                  success(res){
                    if(res.data == false){
                        that.onHideLoading();
                        wx.showModal({
                            showCancel:false,
                            title:"请求失败,请重试",
                        });
                    }
                    else{
                        //成功获取到信息后的操作
                        console.log(res);
                        that.onHideLoading();
                        that.onReloadRoom();
                    }
                  },
                  fail(res){
                      that.onHideLoading();
                    wx.showModal({
                        showCancel:false,
                        title:"请求超时,请重试",
                    });
                  },
                });
            }
            });
        }
        else if(e.currentTarget.dataset.state == 0){
            wx.navigateTo({
              url: '/pages/detailed-room-page/detailed-room-page?roomid='+that.data.rooms[index].room_id,
            })
        }
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
    onReloadRoom:function(e){
        var that = this;
        that.onShowLoading();
        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"GET_PERSON_ROOM",
            studentId:getApp().globalData.userInfo.studentId,
          },
          success(res){
            if(res.data == false){
                that.onHideLoading();
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                that.onHideLoading();
                //成功获取到信息后的操作
                that.setData({
                    "rooms":res.data.roomList,
                })
            }
          },
          fail(res){
              that.onHideLoading();
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    }
})